﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChuyenDuLichDAO
    {
        #region 1. Inserting
        public static bool InsertChuyenDuLich(ChuyenDuLichDTO chuyenDL)
        {
            bool result = false;

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaChuyenDuLich", chuyenDL.MaChuyenDL));
                sqlParams.Add(new SqlParameter("@ThoiGianKhoiHanh", chuyenDL.ThoiGianKhoiHanh));
                sqlParams.Add(new SqlParameter("@TongThoiGian", chuyenDL.TongThoiGian));
                sqlParams.Add(new SqlParameter("@TuyenDuLich", chuyenDL.TuyenDL));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertChuyenDuLich", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                
            }

            return result;
        }
        #endregion
        #region 2. Updating
        public static bool UpdateChuyenDuLich(ChuyenDuLichDTO chuyenDL)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaChuyenDL", chuyenDL.MaChuyenDL));
                sqlParams.Add(new SqlParameter("@ThoiGianKhoiHanh", chuyenDL.ThoiGianKhoiHanh));
                sqlParams.Add(new SqlParameter("@TongThoiGian", chuyenDL.TongThoiGian));
                sqlParams.Add(new SqlParameter("@TuyenDL", chuyenDL.TuyenDL));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateChuyenDuLich", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Deleting
        public static bool DeleteChuyenDuLichByMaChuyenDL(String maChuyenDL)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaChuyenDL", maChuyenDL));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteChuyenDuLichesByAndMaChuyenDL", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion


        #region 4. Retrieving
        public static List<ChuyenDuLichDTO> SelectChuyenDuLichByTuyenDL(int tuyenDL)
        {
            List<ChuyenDuLichDTO> list = new List<ChuyenDuLichDTO>();
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@TuyenDuLich", tuyenDL));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectChuyenDuLichesByAndTuyenDuLich", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    ChuyenDuLichDTO chuyenDL = new ChuyenDuLichDTO();
                    chuyenDL.MaChuyenDL = dr["MaChuyenDuLich"].ToString();
                    chuyenDL.TuyenDL = int.Parse(dr["TuyenDuLich"].ToString());
                    chuyenDL.ThoiGianKhoiHanh = DateTime.Parse(dr["ThoiGianKhoiHanh"].ToString());
                    chuyenDL.TongThoiGian = float.Parse(dr["TongThoiGian"].ToString());
                    list.Add(chuyenDL);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static bool CheckChuyenDuLichByMaChuyenDL(String maChuyenDL)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaChuyenDuLich", maChuyenDL));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectChuyenDuLichesByAndMaChuyenDL", sqlParams);
                if (dt.Rows.Count == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static List<ChuyenDuLichDTO> SelectThoiGianKhoiHanh()
        {
            List<ChuyenDuLichDTO> list = new List<ChuyenDuLichDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectThoiGianKhoiHanh");
                foreach (DataRow dr in dt.Rows)
                {
                    ChuyenDuLichDTO chuyenDL = new ChuyenDuLichDTO();
                    chuyenDL.ThoiGianKhoiHanh = DateTime.Parse(dr["ThoiGianKhoiHanh"].ToString());
                    list.Add(chuyenDL);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
        #endregion
    }
}
